package cst.a729yt.zyfypt_729yt.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

import cst.a729yt.zyfypt_729yt.R;
import cst.a729yt.zyfypt_729yt.ResModel;
import cst.a729yt.zyfypt_729yt.ytListener;

public class RegActivity extends AppCompatActivity {
    private EditText username;
    private EditText password;
    private EditText repassword;
    private EditText realusername;
    private EditText email;
    private EditText tel;
    private RadioButton rbtn_student;
    private RadioButton rbtn_teacher;
    private Button btn_reg;
    private Button btn_relogin;
    private ytListener<String> listener;
    private ResModel model;
    private Intent intent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reg);
        username=(EditText)findViewById(R.id.reg_username);
        password=(EditText)findViewById(R.id.reg_password);
        repassword=(EditText)findViewById(R.id.reg_repassword);
        realusername=(EditText)findViewById(R.id.reg_realusername);
        email=(EditText)findViewById(R.id.reg_Eamil);
        tel=(EditText)findViewById(R.id.reg_tel);
        rbtn_student=(RadioButton)findViewById(R.id.reg_rbtn_student);
        rbtn_teacher=(RadioButton)findViewById(R.id.reg_rbtn_teacher);
        btn_reg=(Button)findViewById(R.id.btn_reg);
        btn_relogin=(Button)findViewById(R.id.btn_reLogin);
        listener=new ytListener<String>() {
            @Override
            public void onResponse(String l) {
                if(l.equals("1")){
                    Toast.makeText(RegActivity.this,"注册成功",Toast.LENGTH_SHORT).show();
                }
                else {
                    Toast.makeText(RegActivity.this,"用户已存在",Toast.LENGTH_SHORT).show();
                }
            }
            @Override
            public void onFail(String msg) {
                Toast.makeText(RegActivity.this,"fuck",Toast.LENGTH_LONG).show();
            }
        };
        btn_reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(password.getText().toString().equals(repassword.getText().toString())){
                    model=new ResModel("scalar");
                    if(rbtn_student.isChecked()){
                        model.reg(username.getText().toString(),password.getText().toString(),tel.getText().toString(),
                                2,email.getText().toString(),realusername.getText().toString(),listener);
                    }
                    else
                        model.reg(username.getText().toString(),password.getText().toString(),tel.getText().toString(),
                                3,email.getText().toString(),realusername.getText().toString(),listener);
                }
                else
                    Toast.makeText(RegActivity.this,"密码不一致",Toast.LENGTH_SHORT).show();
            }
        });
        btn_relogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                intent=new Intent(
                        RegActivity.this,LoginActivity.class
                );
                startActivity(intent);
                finish();
            }
        });
    }

}
